package view3.zjitc.net.androidhomeworkview3;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;

/**
 * @author 朱志文
 * @data 2019/10/13 0013
 */
public class TextViewGroup extends ViewGroup {
    View mChild;
    public TextViewGroup(Context context) {
        super(context);
    }

    public TextViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TextViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec,heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        View child=getChildAt(0);
        child.layout(0,0,child.getMeasuredWidth(),child.getMeasuredHeight());
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                int x= (int) event.getX();
                int y= (int) event.getY();
//                setTranslationX(x);
//                setTranslationY(y);
                useTranslationAnimation(mChild,x,y);
                break;
        }
        return super.onTouchEvent(event);
    }

    private void useTranslationAnimation(View mChild, int x, int y) {
        TranslateAnimation translateAnimation=new TranslateAnimation(0,x,0,y);
        translateAnimation.setDuration(500);
        translateAnimation.setFillAfter(true);
        mChild.startAnimation(translateAnimation);
    }


}
